<?php

	class Devmod_helper {

		/**
		 * converty any input to html signs, restore tags. (so now br's are possible)
		 * @param $text [string] abcdéfg\n\rtest123
		 */
		public function decode_save($text)
		{
			$text = utf8_decode($text);
          	$text = htmlentities($text, ENT_QUOTES);
          	$text = str_replace(array(
          		"&lt;", "&gt;", "quaot;", "&amp;"
          	), array(
          		"<", ">", '"', "&"
          	), $text);
          	
          	return $text;
		}
		
		/**
		 * convert an array to a object. so all the methods have the same returnal type [object]
		 */
		public function array_to_object($array)
		{
			if(!is_array($array) || count($array) == 0) return $array;
			
			$return = new stdClass();
			
			foreach ($array as $k => $v)
			{
				if (is_array($v))
				{
					$return->$k = $this->array_to_object($v);
				}
				else {
					$return->$k = $v;
				}
			}
		 
			return $return;
		}
		
		public function object_to_array($obj)
		{ 
			if(!is_object($obj) || is_null($obj)) return $obj;
			
		    $_arr = is_object($obj) ? get_object_vars($obj) : $obj; 
		    
		    $arr = array();
		    
		    foreach ($_arr as $key => $val)
		    { 
		        $val = (is_array($val) || is_object($val)) ? $this->object_to_array($val) : $val; 
		        $arr[$key] = $val; 
		    } 
		    return $arr; 
		}
		
		public function output_to_array($output)
		{
			$output = $this->object_to_array($output);
			
			foreach($output as $key => $value)
			{
				$output[$key] = $this->object_to_array($value);
			}
			
			return $output;
		}
		
		public function is_valid_config($keys, $to_check_array)
		{
			foreach($keys as $value)
			{
				if(!array_key_exists($value, $to_check_array))
				{
					new Yomm_Error("devmod: the config parameter '$value' was not found in config: " . print_r($to_check_array, true));
					return false;
				}
			}
			return true;
		}
		
		/*
		 * changed the %%value%% of the array within $value_for_value vars
		 * 
		 * @param $value_for_value [string] 'the new text'
		 * @param $to_search_array [array] array('Hello i am %%value%%', 'but i will not be %%replaced%%');
		 */
		public function eval_value($value_for_value, $to_search_array, $place_holder = "%%value%%")
		{
			foreach($to_search_array as $k => $v)
			{
				$replace = str_replace($place_holder, $value_for_value, $v);
				$to_search_array[$k] = (is_numeric($replace)) ? (int) $replace : $replace ;
			}
			return $to_search_array;
		}

		/**
		 * find all variables with %%__VAR__%% in $to_match_content string and replace them with (if existing) in $values_array
		 * 
		 * @param $to_match_content [string] 'i am %%name%% and i life in %%city%%'
		 * @param $values_array [array] array('name' => 'john doe', 'city' => 'New York');
		 */
		public function eval_vars($to_match_content, $values_array)
		{
			if(is_string($values_array))
				return $to_match_content;
			
			$values_array = (array) $values_array;
			
			preg_match_all("/%%([a-zA-Z0-9\.\_]+)%%/", $to_match_content, $result);
			
			foreach($result[1] as $field)
			{
				$expl = explode(".", $field);
				
				if(count($expl) == 1)
				{
					if(array_key_exists($field, $values_array))
					{
						$to_match_content = str_replace("%%$field%%", $values_array[$field], $to_match_content);
					}
				} else {
					if(array_key_exists($expl[0], $values_array))
					{
						if(array_key_exists($expl[1], $values_array[$expl[0]]))
						{
							$to_match_content = str_replace("%%$field%%", $values_array[$expl[0]][$expl[1]], $to_match_content);
						}
					}
				}
			}
			return $to_match_content;
		}
		
		public function array_push_associative(&$arr)
		{
			$ret = 0;
			$args = func_get_args();
			foreach ($args as $arg) {
			     if (is_array($arg)) {
			         foreach ($arg as $key => $value) {
			             $arr[$key] = $value;
			             $ret++;
			         }
			     }else{
					$arr[$arg] = "";
				}
			}
			return $ret;
		}
		
		public function element_build($dom_name, $args)
		{
			$options = null;
			
			foreach($args as $name => $value)
			{
				$options .= "$name=\"$value\" ";
			}
			
			$this->tmp_dom = "<$dom_name $options>[EMPTY]</$dom_name>";
			
			return $this;
		}
		
		public function element_update($value)
		{
			return str_replace("[EMPTY]", ucfirst($value), $this->tmp_dom);
		}
		
		/**
		 * @param $tag [string] html element name. (ex. a)
		 * @param $args [array] attributes (ex. array('href' => 'http://www.google.ch', 'target' => '_blank'))
		 * @param $args [string] content of element
		 * @example
		 * 
		 * echo element('a', array('href' => 'http://www.avancecms.ch'), 'hier klicken');
		 * 
		 * echo element('img', array('src' => 'my_image.png'));
		 */
		public function element($tag, array $args, $value = false)
		{
			$options = '';
			
			foreach($args as $name => $content)
			{
				$options .= "$name=\"$content\" ";
			}
			
			$options = trim($options);
			
			if($value === false) {
				return "<$tag $options />";
			}
			return "<$tag $options>$value</$tag>";
		}
		
	}
